package BackTracking;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;

public class p491递增子序列 {
    class Solution {
        LinkedList<List<Integer>> ans = new LinkedList<>();
        LinkedList<Integer> path = new LinkedList<>();
        public List<List<Integer>> findSubsequences(int[] nums) {
            backtracking(nums, 0);
            return ans;
        }

        public void backtracking(int[] nums, int startIndex){
            if(path.size() > 1){
                ans.add(new LinkedList(path));
            }
            // 每次递归进来都是新的set【即只是控制每层不能有相同元素】
            HashSet<Integer> set = new HashSet<>();
            for(int i = startIndex; i < nums.length; i++){
                // set.contains(nums[i])【树层去重】；
                // !path.isEmpty() && nums[i] < path.get(path.size() - 1
                //【路径不为空，且当前节点的值要大于等于上一个路径结点的值才可加入路径】
                if(set.contains(nums[i]) ||
                        !path.isEmpty() && nums[i] < path.get(path.size() - 1)) continue;
                path.add(nums[i]);
                set.add(nums[i]);
                backtracking(nums, i + 1);
                path.removeLast();
            }
        }
    }
}
